<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_link} function plugin
 *
 * File:       function.pp_link.php<br>
 * Type:       function<br>
 * Name:       pp_link<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of text input types<br>
 * Input:<br>
 *           - name       (optional) - string default "text"
 *           - values     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each text button
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_link values=$ids output=$names}
 * {pp_link values=$ids name='box' separator='<br>' output=$names}
 * {pp_link values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.texts.php {pp_link}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_link($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

	$text = '';
	$link = '';
	$param = '';
	$title = '';
	$target = '';
	$extra = '';
	
	foreach($params as $_key => $_val) {
		switch($_key) {
			case 'text':
				$text = (string)$_val;
				break;
				
			case 'link':
				$link = (string)$_val;
				break;
				
			case 'param':
				$param = (string)$_val;
				break;
			
			case 'title':
				$link = (string)$_val;
				break;
			
			case 'target':
				switch($_val) {
					case '0':
					case 'blank':
					case '_blank':
						$target = '_blank';
						break;
					case '1':
					case 'parent':
					case '_parent':
						$target = '_parent';
						break;
					case '2':
					case 'self':
					case '_self':
						$target = '_self';
						break;
					case '3':
					case 'top':
					case '_top':
						$target = '_top';
						break;
				}
				break;

			case 'assign':
				break;
			
			default:
				if( !is_array($_val) ) {
					$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
				} else {
					$smarty->trigger_error("pp_link: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
				}
				break;
		}
	}

	//
	$_html_result = smarty_function_pp_link_output($text, $link, $param, $title, $target, $extra);

	return $_html_result;
}

function smarty_function_pp_link_output($text, $link, $param, $title, $target, $extra) {
	global $LANGUAGE;
	
	if( substr($text, 0, strlen(':')) == ':' ) {
		$ks = explode(':', $text);
		if( sizeof($ks) == 2 ) {
			$text = isset($LANGUAGE[$ks['1']]) ? $LANGUAGE[$ks['1']] : $ks['1'];
		} else {
			$text = isset($LANGUAGE[$ks['1']][$ks['2']]) ? $LANGUAGE[$ks['1']][$ks['2']] : $ks['2'];
		}
	}
	if( strlen($title) > 0 && substr($title, 0, strlen(':')) == ':' ) {
		$ks = explode(':', $title);
		if( sizeof($ks) == 2 ) {
			$title = isset($LANGUAGE[$ks['1']]) ? $LANGUAGE[$ks['1']] : $ks['1'];
		} else {
			$title = isset($LANGUAGE[$ks['1']][$ks['2']]) ? $LANGUAGE[$ks['1']][$ks['2']] : $ks['2'];
		}
	}
	
	//
	if( $param != '' ) {
		if( $link == '' ) {
			$link .= '?' . $param;
		} elseif( strpos($link, '?') !== false ) {
			$link .= '&' . $param;
		} else {
			$link .= '?' . $param;
		}
	}
	
	//
	$_output = '';
	
	$_output .= '<a href="' . $link . '"';
	if( $title != '' ) {
		$_output .= ' title="' . $title . '"';
	}
	if( $target != '' ) {
		$_output .= ' target="' . $target . '"';
	}
	$_output .= $extra;
    $_output .= '>' . smarty_function_escape_special_chars($text) . '</a>';

    return $_output;
}
//
?>
